home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBFileTransfer.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  4.9 KB  |  163 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBFileTransfer(direction[,fileName]) -- Transfer a file. The direction parameter specified the
  3.         transfer direction ("send" or "receive"). The fileName parameter specifies the file to be sent
  4.         or received; if it is absent or empty, then the user is queried for the file name. Return the
  5.         name of the file actually sent or received.
  6.  
  7.     To compile and link this file using Macintosh Programmer's Workshop,
  8.  
  9.         pascal -w CTBFileTransfer.p
  10.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=2764 -sn Main=CTBFileTransfer ∂
  11.             CTBFileTransfer.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  12.  
  13.     © Copyright 1990 by Apple Computer, Inc.
  14.  
  15.     Initial coding 2/90 by Harry R. Chesley.
  16. *)
  17.  
  18. {$R-}
  19.  
  20. {$S CTBFileTransfer }     { Segment name must be the same as the command name. }
  21.  
  22. unit DummyUnit;
  23.  
  24. interface
  25.  
  26. uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
  27.  
  28. procedure EntryPoint(paramPtr: XCmdPtr);
  29.     
  30. implementation
  31.  
  32. procedure CTBFileTransfer(paramPtr: XCmdPtr); forward;
  33.  
  34. function sendProc(thePtr: Ptr; theSize: longInt; refCon: longInt; channel: CMChannel;
  35.                                 flags: integer): longInt; forward;
  36.  
  37. function recvProc(thePtr: Ptr; theSize: longInt; refCon: longInt; channel: CMChannel;
  38.                                 var flags: CMFlags): longInt; forward;
  39.  
  40. function environsProc(refCon: longInt; var theEnvirons: ConnEnvironRec): CMErr; forward;
  41.  
  42. procedure EntryPoint(paramPtr: XCmdPtr);
  43.  
  44.     begin
  45.         CTBFileTransfer(paramPtr);
  46.     end;
  47.  
  48. procedure CTBFileTransfer(paramPtr: XCmdPtr);
  49.  
  50.     {$I CTBUtil.inc}
  51.  
  52.     var i, j: integer;
  53.         ch: Char;
  54.         sendIt: boolean;
  55.         ft: FTHandle;
  56.         where: Point;
  57.         f: SFReply;
  58.         tl: SFTypeList;
  59.         s: Str255;
  60.         p: Ptr;
  61.  
  62.     procedure Fail(errMsg: Str255); { set theResult and quit }
  63.         begin
  64.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  65.             exit(CTBFileTransfer);
  66.         end;
  67.  
  68.     begin
  69.         { Check the parameter count. }
  70.         i := paramPtr^.paramCount;
  71.         if (i = 0) or (i > 2) then Fail('Invalid parameter count');
  72.  
  73.         { Check that the Comm Toolbox is here. }
  74.         CTBReady;
  75.         { And there's a connection tool. }
  76.         EnsurePresent(connectionTool);
  77.         { And a file transfer tool. }
  78.         EnsurePresent(fileTransferTool);
  79.         { And the connection is open. }
  80.         EnsureOpen;
  81.  
  82.         { Figure out if we're sending or receiving. }
  83.         ch := Chr(paramPtr^.params[1]^^);
  84.         if (ch = 's') or (ch = 'S') then sendIt := true
  85.         else sendIt := false;
  86.  
  87.         { Get the file name. }
  88.         if ParmPresent(2) then
  89.             begin
  90.                 { Get it from the input parameter. }
  91.                 GetStrParm(2,s);
  92.                 if length(s) > 63 then Fail('File name too long');
  93.                 f.vRefNum := 0;
  94.                 f.version := 0;
  95.                 f.fName := s;
  96.             end
  97.         else
  98.             begin
  99.                 { Get it from the user. }
  100.                 where.h := 10; where.v := 40;
  101.                 if sendIt then SFGetFile(where,'File to send:',nil,-1,tl,nil,f)
  102.                 else SFPutFile(where,'File to receive:','file name',nil,f);
  103.                 if not f.good then Fail('User cancel');
  104.             end;
  105.  
  106.         { Open a new tool, so we can set the send/recv/environs proc pointers. Note: In theory, this
  107.             could be done by reaching into the connection tools handle and changing the ProcPtrs
  108.             ourselves (the CTB people tell me it's OK), but I just can't bring myself to do that... This way
  109.             everything'll work in the future regardless of what changes internally, and it really isn't
  110.             all that inefficient, if you think about how many file transfers per second most people do... }
  111.         ft := FTNew(Globals^^.FTHand^^.procID,ftNoMenus+ftQuiet,@sendProc,@recvProc,nil,nil,
  112.                                                         @environsProc,nil,ord4(Globals^^.connHand),0);
  113.         p := FTGetConfig(Globals^^.FTHand);
  114.         j := FTSetConfig(ft,p);
  115.         DisposPtr(p);
  116.  
  117.         { Do the file transfer. Note: some user feedback really ought to be added here. }
  118.         if sendIt then FailOSErr(FTStart(ft,ftTransmitting,f))
  119.         else FailOSErr(FTStart(ft,ftReceiving,f));
  120.         while BAnd(ft^^.flags,ftIsFTMode) <> 0 do FTExec(ft);
  121.  
  122.         { Remember the file name. }
  123.         s := ft^^.theReply.fName;
  124.  
  125.         { Dispose of the file transfer tool we just created. }
  126.         FTDispose(ft);
  127.  
  128.         { Return the file name. }
  129.         paramPtr^.returnValue := PasToZero(paramPtr,s);
  130.     end;
  131.  
  132. function sendProc(thePtr: Ptr; theSize: longInt; refCon: longInt; channel: CMChannel;
  133.                                 flags: integer): longInt;
  134.     { Send a block for the file transfer tool. }
  135.  
  136.     begin
  137.         { Send it to the connection tool in the refCon. }
  138.         sendProc := 0;
  139.         if CMWrite(ConnHandle(refCon),thePtr,theSize,channel,false,nil,0,flags) = noErr then
  140.             sendProc := theSize;
  141.     end;
  142.  
  143. function recvProc(thePtr: Ptr; theSize: longInt; refCon: longInt; channel: CMChannel;
  144.                                 var flags: CMFlags): longInt;
  145.     { Receive a block for the file transfer tool. }
  146.  
  147.     begin
  148.         { Receive it from the connection tool in the refCon. }
  149.         recvProc := 0;
  150.         if CMRead(ConnHandle(refCon),thePtr,theSize,channel,false,nil,0,flags) = noErr then
  151.             recvProc := theSize;
  152.     end;
  153.  
  154. function environsProc(refCon: longInt; var theEnvirons: ConnEnvironRec): CMErr;
  155.     { Get the environment information for the file transfer tool. }
  156.  
  157.     begin
  158.         { Query the connection tool in the refCon for it. }
  159.         environsProc := CMGetConnEnvirons(ConnHandle(refCon),theEnvirons);
  160.     end;
  161.  
  162. end.
  163.